package com.feihao.zhou.web.controller.system;

import com.feihao.zhou.bean.story.Company;
import com.feihao.zhou.bean.system.Dept;
import com.feihao.zhou.service.story.CompanyService;
import com.feihao.zhou.service.story.Impl.CompanyServiceImpl;
import com.feihao.zhou.service.system.DeptService;
import com.feihao.zhou.service.system.Impl.DeptServiceImpl;
import com.feihao.zhou.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@WebServlet("/system/dept")
public class DeptServlet extends HttpServlet {
    static DeptService deptService = new DeptServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text.html;charset=utf-8");

        //获取请求option：
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            //查询方法：
            findAll(req,resp);
        }else if ("toAdd".equals(operation)){
            add(req,resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            update(req,resp);
        }else if ("delete".equals(operation)){
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        deptService.delete(id);
        //重定向到查询：
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Dept dept = BeanUtil.fillBean(req, Dept.class);

        deptService.update(dept);

        //重定向到查询：
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");

    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Dept dept = deptService.findById(id);
        //把部门所有信息加载到update.jsp:
        List<Dept> all = deptService.findAll();
        req.setAttribute("dept",dept);
        req.setAttribute("deptList",all);

        req.getRequestDispatcher(req.getContextPath()+"/WEB-INF/system/dept/update.jsp").forward(req,resp);

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //将数据获取到，封装成一个对象
        Dept dept = BeanUtil.fillBean(req,Dept.class,"yyyy-MM-dd");
        deptService.save(dept);

        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
       /* resp.sendRedirect(req.getContextPath()+"/WEB-INF/system/dept/add.jsp");*/
        //加载所有的部门信息放入到deptList
        List<Dept> all = deptService.findAll();
        req.setAttribute("deptList",all);
        //必须请求转发才行：：！！！！
        req.getRequestDispatcher("/WEB-INF/system/dept/add.jsp").forward(req,resp);
    }


    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //首先定义page,pageSize:
        int page = 1;
        int pageSize = 5;

        if(StringUtils.isNotBlank(req.getParameter("page"))){
            page = Integer.parseInt(req.getParameter("page"));
        }

        //调用业务层方法查询所有：
        PageInfo all = deptService.findAll(page, pageSize);

        System.out.println(all);
        req.setAttribute("page",all);
        req.getRequestDispatcher("/WEB-INF/system/dept/list.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
